﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.Admin.Advertise
{
    public class OpenAdvertiseService : IOpenAdvertiseService
    {
        Application.Admin.Advertise.IAdvertisePicService adPicService;

        Application.Admin.Advertise.IAdvertiseService adService;

        Application.Admin.Pic.IPicInfoService picService;

        public OpenAdvertiseService(Application.Admin.Advertise.IAdvertisePicService adPic,
            Application.Admin.Advertise.IAdvertiseService ad,
            Application.Admin.Pic.IPicInfoService pic)
        {
            if (adPic == null)
            {
                throw new ArgumentNullException("adPicService is null");
            }
            if (ad == null)
            {
                throw new ArgumentNullException("adService is null");
            }
            if (pic == null)
            {
                throw new ArgumentNullException("picService is null");
            }
            adPicService = adPic;
            adService = ad;
            picService = pic;
        }

        public Dto.OpenAdvertiseDto GetRandOneAdvertise()
        {
            Dto.OpenAdvertiseDto data = null;
            var count = this.GetOpenAdvertiseDtoTrueState().Count();
            if (count > 0)
            {
                if (count == 1)
                {
                    data = this.GetOpenAdvertiseDtoTrueState().FirstOrDefault();
                }
                else
                {
                    Random rand = new Random();
                    var temp = rand.Next(0, count);
                    if (temp < 0)
                    {
                        temp = 0;
                    }
                    if (temp == count)
                    {
                        temp = count - 1;
                    }
                    data = this.GetOpenAdvertiseDtoTrueState().Skip(temp).Take(1).FirstOrDefault();
                }
            }
            return data;
        }

        public Dto.OpenAdvertiseDto GetAdvertiseById(int id)
        {
            var data = this.GetOpenAdvertiseDtoTrueState().Where(e => e.Id == id).FirstOrDefault();
            return data;
        }

        public List<Dto.OpenAdvertiseDto> GetAllAdvertise()
        {
            var data = this.GetOpenAdvertiseDtoTrueState().ToList();
            return data;
        }

        #region util

        protected IQueryable<Application.Admin.Dto.OpenAdvertiseDto> GetOpenAdvertiseDtoTrueState()
        {
            var now = System.DateTime.Now;
            var data = (from adpic in adPicService.GetList()
                        join ad in adService.GetList() on adpic.AdverseId equals ad.Id
                        join pic in picService.GetList() on adpic.PicInfoId equals pic.Id
                        where ad.State == true && ad.ExpiredDateTime >= now
                        orderby ad.Id descending
                        select new Application.Admin.Dto.OpenAdvertiseDto
                        {
                            Id = ad.Id,
                            ImagePath = pic.WebFullPath,
                            Url = ad.Url
                        });
            return data;
        }

        #endregion
    }
}
